<?php
/**
 * Created by PhpStorm.
 * User: 
 * Date: 
 * Time: 
 */

namespace app\services\model;

use think\Config;
use think\Model;
use think\Db;

class CaseChange extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'case_change';

    /**
     * [addOneChange description]
     * @param  [type] $userId  [description]
     * @param  [type] $reqData [description]
     * @return [type]          [description]
     */
    public function addOneChange($userId,$reqData){
        $caseId = $reqData['caseId'];
        $caseChange = $reqData['caseChange'];
        if(!is_array($caseChange)){
            return 3007;
        }
        $dateTime = date("Y-m-d H:i:s");  //这一批数据，变更时间相同
        Db::startTrans();
        $column_count = 0;
        $roleOb = new \app\services\model\CaseList();
        //查询病例的元素的的当前值
        $allElement = $roleOb->elementDetail($caseId);

        $totalColumnCount = $roleOb->queryElementCount($caseId);
        foreach ($caseChange as $key => $value) {            
            $key = $value['key'];
            $after = $value['value'];
            $reason = empty($value['reason'])?"":$value['reason'];
            foreach ($allElement as $key1 => $value1) {
                if($value1['key']==$key){
                    if($value1['value']==$after){
                        //不去操作，前一次跟后一次并没有改变
                    }
                    else{
                        //2 入库
                        $ob = new \app\services\model\CaseChange([
                            "case_id"=>$caseId,
                            "user_id"=>$userId,
                            "before"=>$value1['value'],
                            "after"=>$after,
                            "q_column"=>$key,
                            "q_reason"=>$reason,
                            "add_time"=>$dateTime,
                        ]);
                        if(!$ob->save()){
                            Db::rollback();
                            return 1004;
                        }
                        //3变更记录
                        $ret = $roleOb->updateElementValue($caseId,$key,$after);
                        if($ret){
                            Db::rollback();
                            return 1004;            
                        }
                    }
                    break;
                }
            }
            // //1查询原值  不要一个个的查询，
            // $beforeValue = $roleOb->queryBeforeElementValue($caseId,$key);
            // if($beforeValue){
            //     $before = $beforeValue['value'];
            // }
            // else{
            //     continue;
            //     // return 3006;
            // }
           
            $column_count++;
        }
        //4添加日志
        $ob2 = new \app\services\model\CaseStatusLog([
            "user_id"=>$userId,
            "case_id"=>$caseId,
            "user_role_name"=>'录入员',//变更默认只有录入员
            "status"=>10,
            "column_count"=>$column_count,
            "column_total_count"=>$totalColumnCount,
            
        ]);
        if(!$ob2->save()){
            Db::rollback();
            trace("case change create case log error");
            return 1004;
        } 

        Db::commit();
        return 0;
    } 
}